import axios from './api.request'
import { Message } from 'iview'

export function httpPost (url, params) {
    const defaultErr = '接口错误'
    let p = new Promise((resolve, reject) => {
        axios.request({
            method: 'post',
            url: url,
            data: params
        }).then(res => {
            let isSuccess = res.data.code === '0000'
            let responseMsg = res.data.msg
            if (isSuccess) {
                resolve(res.data)
            } else {
                responseMsg ? Message.error(responseMsg) : Message.error(defaultErr)
                resolve(res.data)
            }
        }).catch(err => {
            err.data && err.data.msg ? Message.error(err.data.msg) : Message.error(defaultErr)
            reject(err)
        })
    })
    return p
}

export function httpGet (url, params) {
    const defaultErr = '接口错误'
    let p = new Promise((resolve, reject) => {
        axios.request({
            method: 'get',
            url: url,
            data: params
        }).then(res => {
            let isSuccess = res.data.code === '0000'
            let responseMsg = res.data.msg
            if (isSuccess) {
                resolve(res.data)
            } else {
                responseMsg ? Message.error(responseMsg) : Message.error(defaultErr)
                resolve(res.data)
            }
        }).catch(err => {
            err.data && err.data.msg ? Message.error(err.data.msg) : Message.error(defaultErr)
            reject(err)
        })
    })
    return p
}
